<template>
  <div class="home-container">
    <!-- 顶部导航栏组件 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- 频道列表 -->
    <van-tabs class="channel-tabs" v-model="active" swipeable animated>
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <articlelist :homelist="item"></articlelist>
      </van-tab>
      <!-- 汉堡按钮 -->
      <div slot="nav-right" class="hamburger-btn" @click="isshow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="isshow"
      close-icon-position="top-left"
      closeable
      position="bottom"
      :style="{ height: '100%' }"
    >
      <channeledit
        :editlist="channels"
        :active="active"
        @update-active="upactive"
      ></channeledit>
    </van-popup>
    <!-- 频道编辑弹出层 -->
  </div>
</template>

<script>
import { getUserChannels } from "@/api/user.js";
import articlelist from "./components/articleList.vue";
import channeledit from "../../components/channel-edit.vue";
import { mapState } from "vuex";
import { getItem } from "@/utils/storage.js";
export default {
  name: "HomeIndex",
  data() {
    return {
      active: 0,
      channels: [], // 频道列表
      isshow: false,
    };
  },
  components: { articlelist, channeledit },

  async created() {
      let channels = []
      //判断有无登录
      if (this.user) {
        //登陆直接拿服务器的数据
        const { data } = await getUserChannels();
        channels = data.data.channels;
      } else {
        //未登录直接拿本地的数据
        const localChannels = getItem("CHANNELS-TOKEN");
        //未登录如果本地有数据拿本地的
        if (localChannels) {
          channels = localChannels;
        } else {
          //未登录 如果本地没有数据拿服务器的
          const { data } = await getUserChannels();
          channels = data.data.channels;
        }
      }
      //最终把数据赋值个data中的数组渲染页面
      this.channels=channels
  },

  methods: {
    upactive(index, inshow) {
      this.active = index;
      this.isshow = inshow;
    },
  },
  computed: {
    ...mapState(["user"]),
  },
};
</script>

<style scoped lang="less">
.home-container {
  padding-top: 174px;
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }

    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777777;
    }

    .van-tab--active {
      color: #333333;
    }

    .van-tabs__nav {
      padding-bottom: 0;
    }

    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
  }
  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;

    i.toutiao {
      font-size: 33px;
    }

    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
</style>
